package com.help10000.rms.ui.windows

import android.content.Context
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.amir.common.beans.TencentMapSearchData
import com.amir.common.holder.CommonViewHolder
import com.amir.common.mvp.base.BasePopuWindow
import com.amir.common.mvp.presenter.BasePresenter
import com.amir.common.mvp.view.IView
import com.help10000.rms.R
import com.help10000.rms.ui.adapter.NavigationSearchAdapter

class SearchKeyPopuWindow(
    context: Context?,
    layoutid: Int,
    data: MutableList<TencentMapSearchData>
) :
    BasePopuWindow<BasePresenter<IView>?, IView?>(context, layoutid) {
    var data: MutableList<TencentMapSearchData> = ArrayList()
    var navigationSearchAdapter: NavigationSearchAdapter? = null
    var holder: CommonViewHolder? = null
    private fun initData() {
        this.isFocusable = false
        this.setOutsideTouchable(true)
        navigationSearchAdapter = NavigationSearchAdapter(mContext, data, R.layout.item_map_search)
        val rv_list = holder!!.getView<RecyclerView>(R.id.rv_list)
        rv_list.layoutManager = LinearLayoutManager(mContext)
        rv_list.adapter = navigationSearchAdapter
        navigationSearchAdapter!!.setOnItemClickListener { pos ->
            val tencentMapSearchData = data[pos]
            if (searchSelectResultListener != null) {
                searchSelectResultListener!!.onResult(tencentMapSearchData)
                dismiss()
            }
        }
    }

    override fun initBindHolder(holder: CommonViewHolder) {
        this.holder = holder
    }

    fun notifyDataSetChanged(data: List<TencentMapSearchData>?) {
        this.data.clear()
        this.data.addAll(data!!)
        navigationSearchAdapter!!.notifyDataSetChanged()
    }

    fun showPopu(data: List<TencentMapSearchData>?, view: View) {
        showAsDropDown(view)
        this.data.clear()
        this.data.addAll(data!!)
        navigationSearchAdapter!!.notifyDataSetChanged()
    }

    override fun createPresenter(): BasePresenter<IView>? {
        return null
    }

    private var searchSelectResultListener: SearchSelectResultListener? = null

    interface SearchSelectResultListener {
        fun onResult(searchData: TencentMapSearchData?)
    }

    fun setSearchSelectResultListener(searchSelectResultListener: SearchSelectResultListener?) {
        this.searchSelectResultListener = searchSelectResultListener
    }

    init {
        this.data = data
        initData()
    }
}